package com.wedo.mall.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.e.library.utils.EDensityUtils;
import com.e.library.utils.EUtils;
import com.wedo.mall.R;
import com.wedo.mall.adapter.AttrAdapter;
import com.wedo.mall.entity.Attr;
import com.wedo.mall.entity.AttrItem;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品属性控件
 * Created by e on 2017/7/11.
 */

public class AttrView extends LinearLayout {

    /**每行2个控件*/
    //private int mSpanCount = 2;
    private int mHeight;
//    private int mWidth;
    private int mMargin;

    private List<View> items = new ArrayList<>();

    private Attr mAttr;
    private ArrayMap<Attr,AttrItem> mSelectedAttrs;
    private AttrAdapter.OnAttrChanged mOnAttrChanged;

    public AttrView(Context context) {
        this(context,null);
    }

    public AttrView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public AttrView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mHeight = EDensityUtils.dip2px(context,28);
//        mWidth = EDensityUtils.dip2px(context,120);
        mMargin = EDensityUtils.dip2px(context,3);
    }

    public AttrView selected(ArrayMap<Attr,AttrItem> selected){
        this.mSelectedAttrs = selected;
        return this;
    }

    public AttrView listener(AttrAdapter.OnAttrChanged onAttrChanged){
        this.mOnAttrChanged = onAttrChanged;
        return this;
    }

    public AttrView attr(Attr attr){
        this.mAttr = attr;
        attrValue(attr.attrValue);
        return this;
    }

    private void attrValue(List<AttrItem> attrItems){
        items.clear();
        setOrientation(VERTICAL);
        removeAllViews();
        if (EUtils.isEmpty(attrItems)){
            return;
        }

        //LinearLayout line = null;
        int count = attrItems.size();
        for (int i = 0; i < count; i++) {
            AttrItem attr = attrItems.get(i);
            attr.attr_name = mAttr.attr_name;
            View item = item(attr);
            items.add(item);
            this.addView(item,params());
//            int value = i % mSpanCount;
//            if (value == 0){
//                line = line();//新增一行
//            }
//
//            line.addView(item,params());
//
//            if (value == 0){
//                this.addView(line);//新增一行添加到View上
//            }
        }
    }

    /**属性单选*/
    private void onItemClick(AttrItem attrItem,View view){
        for (View item:items){
            if (item !=view){
                item.setSelected(false);
            }else {
                if (view.isSelected()){
                    view.setSelected(false);
                    mSelectedAttrs.remove(mAttr);
                }else {
                    view.setSelected(true);
                    mSelectedAttrs.put(mAttr,attrItem);
                }
            }
        }

        if (mOnAttrChanged !=null){
            mOnAttrChanged.selected(mSelectedAttrs);
        }
    }

    private Button item(final AttrItem attrItem){
        final Button button = (Button) LayoutInflater.from(getContext()).inflate(R.layout.widget_attr_button,null);
        button.setText(attrItem.attr_value);
        button.setSelected(mSelectedAttrs.containsValue(attrItem));
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClick(attrItem,v);
            }
        });

        return button;
    }

    private LayoutParams params(){
        //LayoutParams params = new LayoutParams(mWidth, mHeight);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, mHeight);
        params.leftMargin = mMargin;
        params.rightMargin = mMargin;
        params.topMargin = mMargin;
        params.bottomMargin = mMargin;
        return params;
    }

    private LinearLayout line() {
        LinearLayout l = new LinearLayout(getContext());
        l.setOrientation(HORIZONTAL);
        LinearLayout.LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        l.setLayoutParams(params);
        return l;
    }


}
